use std::cmp::Ordering;
use std::io;
use rand::Rng;

fn main() {
    println!("猜数字游戏开始....");

    println!("请输入数字...");

    let secret_number = rand::thread_rng().gen_range(1, 100);

    //无限循环
    loop {
        //声明一个可变变量    :: 是关联函数， 类似java中的静态函数
        let mut guess_num = String::new();

        let res = std::io::stdin().read_line(&mut guess_num).expect("读取错误, 退出。。。");

        println!("res :{}", res);

        println!("数字是:{}", guess_num);

        let guess_num: i32 = match guess_num.trim().parse() {
            Ok(num) => num, //
            Err(_) => {
                println!("请输入数字。。。");
                continue;
            } //
        };

        // 比较输入的数字和生成的随机数字
        match guess_num.cmp(&secret_number) {
            Ordering::Less => println!("太小了"),
            Ordering::Greater => println!("太大了!"),
            Ordering::Equal => {
                println!("你猜对了!");
                break;
            }
        }
    }
}
